<template>
  <view class="category_tab">
    <view class="category_tab_title">
      <view class="category_tab_inner">
        <uni-segmented-control
          :current="current"
          :values="items.map((item) => item.title)"
          @clickItem="onClickItem"
          style-type="text"
          active-color="#42a5f5"
        ></uni-segmented-control>
      </view>
      <view class="iconfont iconsearch"></view>
    </view>
    <!-- 设置 enable-flex 属性以使 flexbox 布局生效 -->
    <scroll-view
      class="category_tab_content"
      scroll-y="true"
      enable-flex
      @scrolltolower="handleScrolltolower"
    >
      <view class="category_item" v-for="item in vertical" :key="item.id">
        <image :src="item.thumb" mode="widthFix" />
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui"; // 导入 uni-ui 的分段器
export default {
  data() {
    return {
      id: "",
      items: [
        { title: "最新", order: "new" },
        { title: "热门", order: "hot" },
      ],
      current: 0,
      // 请求参数
      params: {
        limit: 30,
        skip: 0,
        order: "new",
      },
      // 图片数据
      vertical: [],
      hasMore: true,
    };
  },
  methods: {
    /* 监听scroll-view标签触底 */
    handleScrolltolower() {
      // console.log("触底啦");
      if (this.hasMore) {
        this.params.skip += this.params.limit;
        this.getList();
      } else {
        uni.showToast({
          title: "没有更多数据了哦",
          duration: 2000,
          icon: "none",
        });
      }
    },
    /* 点击分段器 item事件 */
    onClickItem(e) {
      // console.log("debug => ",e) {currentIndex: 1}
      if (this.current !== e.currentIndex) {
        this.current = e.currentIndex;
      }else{
          // 点击的相同的标题
          return;
      }

      /*
        1、切换标题
        2、切换order
         */
      uni.setNavigationBarTitle({
        title: this.items[e.currentIndex].title,
      });
      this.params.order = this.items[e.currentIndex].order;
      this.params.skip = 0;
      this.vertical = [];

      this.getList();
    },
    getList() {
      this.request({
        url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
        data: this.params,
      }).then((result) => {
        // console.log(result);
        if (result.res.vertical.length === 0) {
          this.hasMore = false;
          uni.showToast({
            title: "没有更多数据了哦",
            duration: 2000,
            icon: "none",
          });
          return;
        }
        this.vertical = [...this.vertical, ...result.res.vertical];
      });
    },
  },
  /* 页面生命周期函数，监听页面加载 */
  onLoad(options) {
    // console.log(options);
    this.id = options.id;
    this.getList();
  },
  components: {
    uniSegmentedControl,
  },
};
</script>

<style lang="scss">
.category_tab {
  .category_tab_title {
    position: relative; // relative 相对定位
    .category_tab_inner {
      width: 60%;
      margin: 0 auto; // 水平居中
    }
    .iconsearch {
      position: absolute; // absolute 绝对定位
      top: 50%;
      transform: translateY(-50%);
      right: 5%;
    }
  }
  .category_tab_content {
    display: flex;
    flex-wrap: wrap;
    height: calc(100vh - 36px); // 必须是屏幕的高度(100vh) - 头部标题的高度
    .category_item {
      border: 3rpx solid #fff;
      width: 33.33%;
      image {
      }
    }
  }
}
</style>